//登录
import { login } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";

const user = {
	//状态
	state: {
		token: getToken(),
		avatar: "",
	},

	//更改vurx的store中的状态的唯一方法就是提交mutations
	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token;
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar;
		},
	},

	/**
	 * action提交mutations，而不是直接变更状态
	 * action可以包含任意异步操作
	 */
	actions: {
		// 调用context.commit提交一个mutation，或者通过context.state和context.getters来获取state和getters
		// 	Login({ commit }, userInfo) {
		// 		const username = userInfo.username.trim();
		// 		const password = userInfo.password;
		// 		const code = userInfo.code;
		// 		const uuid = userInfo.uuid;
		// 		return new Promise((resolve, reject) => {
		// 			login(username, password, code, uuid)
		// 				.then((res) => {
		// 					setToken(res.token);
		// 					commit("SET_TOKEN", res.token);
		// 					resolve();
		// 				})
		// 				.catch((error) => {
		// 					reject(error);
		// 				});
		// 		});
		// 	},
	},
};

export default user;
